package dragon.modules.system.user.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;

import dragon.core.exception.ExceptionHandler;
import dragon.core.system.dao.UserGroupDao;
import dragon.core.system.po.SysUser;
import dragon.core.system.po.SysUserGroup;
import dragon.core.system.po.SysUserGroupLink;
import dragon.core.system.po.SysUsermanagementroles;
import dragon.core.util.Handler;
import dragon.modules.system.user.dao.UserDaoImpl;
import dragon.modules.system.user.dao.UserGroupDaoImpl;
import dragon.modules.system.user.dao.UserGroupLinkDaoImpl;
import dragon.modules.system.user.dao.UserManagementRolesDao;

public class UserGroupHandler extends Handler {
	private UserGroupDao ugd = new UserGroupDaoImpl();

	private UserGroupLinkDaoImpl ugld = new UserGroupLinkDaoImpl();

	private UserManagementRolesDao umrd = new UserManagementRolesDao();

	private UserDaoImpl uDao = new UserDaoImpl();

	public String loadGroupUsers() throws ExceptionHandler {
		String gid = getParameter("id").trim();
		List<SysUser> groupUsers = ugld.loadGroupUsers(Integer.parseInt(gid));
		List<SysUser> sysUsers = new ArrayList<SysUser>();
		if (groupUsers == null || groupUsers.size() == 0) {
			sysUsers = uDao.loadAllUser();
		} else {
			int[] id = new int[groupUsers.size()];
			for (int i = 0; i < groupUsers.size(); i++) {
				id[i] = groupUsers.get(i).getId();
			}
			sysUsers = uDao.loadNotMarkUser(id);
		}
		setAttribute("glist", groupUsers);
		setAttribute("slist", sysUsers);
		setAttribute("gid", gid);
		return "/webmodules/system/usergroup/groupUserList.jsp";
	}

	public void addGroupUser() throws ExceptionHandler {
		SysUserGroupLink uLink = new SysUserGroupLink();
		uLink.setUserid(Integer.parseInt(getParameter("uid").trim()));
		uLink.setUsergroupid(Integer.parseInt(getParameter("gid").trim()));
		output(ugld.saveUserGroupLink(uLink) ? successJSON : failureJSON);
	}

	public void delGroupUser() throws ExceptionHandler {
		SysUserGroupLink uLink = new SysUserGroupLink();
		uLink.setUserid(Integer.parseInt(getParameter("uid").trim()));
		uLink.setUsergroupid(Integer.parseInt(getParameter("gid").trim()));
		output(ugld.deleteUserGroupLink(uLink) ? successJSON : failureJSON);
	}

	/**
	 * 添加用户权限
	 * 
	 * @throws ExceptionHandler
	 */
	public void addUserGroupLink() throws ExceptionHandler {
		int uid = Integer.parseInt(getParameter("uid").trim());
		String gid = getParameter("roles").trim();
		ugld.deleteUserGroupLink(uid);
		String[] gids = gid.split(",");
		if (!gids[0].equals("")) {
			for (String g : gids) {
				SysUserGroupLink sugl = new SysUserGroupLink();
				sugl.setUserid(uid);
				sugl.setUsergroupid(Integer.parseInt(g));
				ugld.saveUserGroupLink(sugl);
			}
		}
		output(successJSON);
	}

	/**
	 * 添加用户可操作作管理的权限
	 * 
	 * @throws ExceptionHandler
	 */
	public void addUserManagementRoles() throws ExceptionHandler {
		int uid = Integer.parseInt(getParameter("uid").trim());
		String gid = getParameter("roles").trim();
		umrd.delete(uid);
		String[] gids = gid.split(",");
		if (!gids[0].equals("")) {
			for (String g : gids) {
				SysUsermanagementroles po = new SysUsermanagementroles();
				po.setUserid(uid);
				po.setUsergroupid(Integer.parseInt(g));
				umrd.save(po);
			}
		}
		output(successJSON);
	}

	/**
	 * 用于前台页面中通过combotree的方式调用
	 * 
	 * @throws ExceptionHandler
	 */
	@SuppressWarnings("unchecked")
	public void combotree() throws ExceptionHandler {
		List<SysUserGroup> usergrouplist = ugd.loadUserManagementGroup(getUser().getId());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (SysUserGroup sd : usergrouplist) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", sd.getId());
			map.put("text", sd.getName());
			list.add(map);
			map = null;
		}
		usergrouplist = null;
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("", list);
		String result = jsonObject.toJSONString();
		result = result.substring(4, result.length() - 1);
		output(result);
		jsonObject = null;
		list = null;
	}

}
